import request from '../../util/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoGroupList:[], //表单导航数据
    navId:'' || 58100, //导航唯一标识
    videoList: [], //视频列表数据
    videoId:'', //视频的id
    videoUpdateTime:[],//记录播放历史视频时间
    isTrigger:false,//下拉刷新是停止数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getVideoGroupListData();
    this.getVideoList(this.data.navId)
  },
  // 获取导航表单数据
  async getVideoGroupListData(){
    let videoGroupListData = await request('/video/group/list')
    this.setData({
      videoGroupList:videoGroupListData.data.slice(0,10)
    })
  },
  // 获取视频列表数据
  async getVideoList(navId){
    let videoListData = await request('/video/group',{id:navId})
    //隐藏加载
    wx.hideLoading({
    })
    let index= 0;
    let videoList = videoListData.datas.map(item => {
      item.id = index++;
      return item
    })
    // console.log(videoListData);
    this.setData({
      videoList,
      isTrigger:false
    })
  },
  

  // 导航点击切换回调函数
  changeNav(e){
    let navId= e.currentTarget.id; 
    this.setData({
      navId ,
      videoList:[]
    })
    // 加载提示
    wx.showLoading({
      title: '正在加载'
    })
    // 获取当前导航数据
    this.getVideoList(this.data.navId)
  },
  // 点击播放的回调
  handlePlay(e){
    let vid = e.currentTarget.id;
    // this.vid!== vid&&this.videoContext && this.videoContext.stop();
    // this.vid = vid
    this.setData({
      videoId: vid
    })
    this.videoContext = wx.createVideoContext(vid)
    let {videoUpdateTime} = this.data;
    let videoItem = videoUpdateTime.find( item => item.vid === vid);
    if(videoItem){
    this.videoContext.seek(videoItem.currentTime)
    }
    this.videoContext.autoplay();
    // this.videoContext.play(); 
  },
  //视频播放记录的回调
  handleUpdateTime(e){
    let videoTimeObj = {vid:e.currentTarget.id, currentTime:e.detail.currentTime};
    let {videoUpdateTime} = this.data;
    let videoItem = videoUpdateTime.find( item => item.vid === videoTimeObj.vid);
    if(videoItem){
     videoItem.currentTime = e.detail.currentTime;
    }else{
      videoUpdateTime.push(videoTimeObj)
    }
    this.setData({
      videoUpdateTime
    })
  },
  // 视频播放结束的回调
  handleEnded(e){
    let {videoUpdateTime} = this.data;
    let index = videoUpdateTime.findIndex(item => item.vid === e.currentTarget.id);
    videoUpdateTime.splice(index,1)
    this.setData({
      videoUpdateTime
    })
  },
  //自定义下拉刷新的回调
  handleRefresher(e){
    this.getVideoList(this.data.navId)
  },
  // 跳转到search页面
  toSearch(){
    wx.navigateTo({
      url: '/pages/search/seaerch',
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})